import { createRouter, createWebHistory } from 'vue-router'
import LogisticsHome from '@/views/LogisticsHome.vue'
import Login from '../views/UserLogin.vue'
import Register from '../views/UserRegister.vue'
import auth from '../utils/auth';
// 商品管理相关组件
import StockList from '../views/product/StockList.vue'
import InOrderList from '../views/product/InOrderList.vue'
import OutOrderList from '../views/product/OutOrderList.vue'
import ProductDetail from '../views/product/ProductDetalil.vue'
import OutOrderDetail from '../views/product/OutOrderDetail.vue'
import InOrderDetail from '../views/product/InOrderDetail.vue'

// 采购管理相关组件
import PurchaseManage from '../views/purchase/PurchaseManage.vue'
import PurchaseOrderDetail from '../views/purchase/OrderDetail.vue'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { title: '用户登录' }
  },
  {
    path: '/register',
    name: 'Register',
    component: Register,
    meta: { title: '用户注册' }
  },
  {
    path: '/',
    name: 'Home',
    component: LogisticsHome,
    meta: { title: '物流管理系统' ,requiresAuth: true},
    
    children: [
      // 采购管理（一级菜单，无子页面）
      {
        path: 'purchaseManage',
        name: 'PurchaseManage',
        component: PurchaseManage,
        meta: { title: '采购管理',requiresAuth: true }
      },
      // 商品管理（一级菜单，包含三个子页面）
      {
        path: 'productManage',
        name: 'ProductManage',
        redirect: '/productManage/stock',
        meta: { title: '商品管理' ,requiresAuth: true},
        children: [
          {
            path: 'stock',
            name: 'StockList',
            component: StockList,
            meta: { title: '商品库存列表',requiresAuth: true}
          },
          {
            path: 'in',
            name: 'InOrderList',
            component: InOrderList,
            meta: { title: '入库订单列表' ,requiresAuth: true}
          },
          {
            path: 'out',
            name: 'OutOrderList',
            component: OutOrderList,
            meta: { title: '出库订单列表' ,requiresAuth: true}
          }
        ]
      },
      // 详情类路由（保持原有配置）
      {
        path: 'productDetail/:id',
        name: 'ProductDetail',
        component: ProductDetail,
        meta: { title: '商品详情' ,requiresAuth: true}
      },
      {
        path: 'outOrderDetail/:id',
        name: 'OutOrderDetail',
        component: OutOrderDetail,
        meta: { title: '出库订单详情' ,requiresAuth: true}
      },
      {
        path: 'inOrderDetail/:id',
        name: 'InOrderDetail',
        component: InOrderDetail,
        meta: { title: '入库订单详情' ,requiresAuth: true}
      },
      {
        path: 'purchaseOrderDetail/:id',
        name: 'PurchaseOrderDetail',
        component: PurchaseOrderDetail,
        meta: { title: '采购订单详情' ,requiresAuth: true}
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 导航守卫（未修改）
router.beforeEach((to, from, next) => {
  const whiteList = ['/login', '/register']
  if (whiteList.includes(to.path)) return next()
    if (to.matched.some(record => record.meta.requiresAuth)) {
      if (!auth.isAuthenticated()) {
        next('/login');
      } else {
        next();
      }
    } else {
      next();
    }
  const hasLogin = localStorage.getItem('token') !== null
  if (!hasLogin) {
    next('/login')
  } else {
    next()
  }
})

export default router